
USE [Quotes]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Authors_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Authors_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Authors_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Authors table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Authors_Get_List

AS


				
				SELECT
					[ID],
					[CountryID],
					[AuthorName],
					[AuthorFirstName],
					[AuthorLastName],
					[AuthorBirthday],
					[AuthorType],
					[AuthorDied],
					[AuthorLinkName],
					[RefID]
				FROM
					[dbo].[Authors]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Authors_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Authors_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Authors_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Authors table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Authors_GetPaged
(

	@WhereClause varchar (8000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[Authors]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ID], O.[CountryID], O.[AuthorName], O.[AuthorFirstName], O.[AuthorLastName], O.[AuthorBirthday], O.[AuthorType], O.[AuthorDied], O.[AuthorLinkName], O.[RefID]
				FROM
				    [dbo].[Authors] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
                
				-- get row count
				SET @SQL = 'SELECT COUNT(1) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Authors]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Authors_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Authors_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Authors_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Authors table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Authors_Insert
(

	@Id int    OUTPUT,

	@CountryId int   ,

	@AuthorName nvarchar (100)  ,

	@AuthorFirstName nvarchar (100)  ,

	@AuthorLastName nvarchar (100)  ,

	@AuthorBirthday datetime   ,

	@AuthorType nvarchar (50)  ,

	@AuthorDied datetime   ,

	@AuthorLinkName nvarchar (1000)  ,

	@RefId int   
)
AS


				
				INSERT INTO [dbo].[Authors]
					(
					[CountryID]
					,[AuthorName]
					,[AuthorFirstName]
					,[AuthorLastName]
					,[AuthorBirthday]
					,[AuthorType]
					,[AuthorDied]
					,[AuthorLinkName]
					,[RefID]
					)
				VALUES
					(
					@CountryId
					,@AuthorName
					,@AuthorFirstName
					,@AuthorLastName
					,@AuthorBirthday
					,@AuthorType
					,@AuthorDied
					,@AuthorLinkName
					,@RefId
					)
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Authors_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Authors_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Authors_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Authors table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Authors_Update
(

	@Id int   ,

	@CountryId int   ,

	@AuthorName nvarchar (100)  ,

	@AuthorFirstName nvarchar (100)  ,

	@AuthorLastName nvarchar (100)  ,

	@AuthorBirthday datetime   ,

	@AuthorType nvarchar (50)  ,

	@AuthorDied datetime   ,

	@AuthorLinkName nvarchar (1000)  ,

	@RefId int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Authors]
				SET
					[CountryID] = @CountryId
					,[AuthorName] = @AuthorName
					,[AuthorFirstName] = @AuthorFirstName
					,[AuthorLastName] = @AuthorLastName
					,[AuthorBirthday] = @AuthorBirthday
					,[AuthorType] = @AuthorType
					,[AuthorDied] = @AuthorDied
					,[AuthorLinkName] = @AuthorLinkName
					,[RefID] = @RefId
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Authors_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Authors_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Authors_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Authors table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Authors_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Authors] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Authors_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Authors_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Authors_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Authors table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Authors_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[CountryID],
					[AuthorName],
					[AuthorFirstName],
					[AuthorLastName],
					[AuthorBirthday],
					[AuthorType],
					[AuthorDied],
					[AuthorLinkName],
					[RefID]
				FROM
					[dbo].[Authors]
				WHERE
					[ID] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Authors_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Authors_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Authors_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Authors table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Authors_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@CountryId int   = null ,

	@AuthorName nvarchar (100)  = null ,

	@AuthorFirstName nvarchar (100)  = null ,

	@AuthorLastName nvarchar (100)  = null ,

	@AuthorBirthday datetime   = null ,

	@AuthorType nvarchar (50)  = null ,

	@AuthorDied datetime   = null ,

	@AuthorLinkName nvarchar (1000)  = null ,

	@RefId int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [CountryID]
	, [AuthorName]
	, [AuthorFirstName]
	, [AuthorLastName]
	, [AuthorBirthday]
	, [AuthorType]
	, [AuthorDied]
	, [AuthorLinkName]
	, [RefID]
    FROM
	[dbo].[Authors]
    WHERE 
	 ([ID] = @Id OR @Id IS NULL)
	AND ([CountryID] = @CountryId OR @CountryId IS NULL)
	AND ([AuthorName] = @AuthorName OR @AuthorName IS NULL)
	AND ([AuthorFirstName] = @AuthorFirstName OR @AuthorFirstName IS NULL)
	AND ([AuthorLastName] = @AuthorLastName OR @AuthorLastName IS NULL)
	AND ([AuthorBirthday] = @AuthorBirthday OR @AuthorBirthday IS NULL)
	AND ([AuthorType] = @AuthorType OR @AuthorType IS NULL)
	AND ([AuthorDied] = @AuthorDied OR @AuthorDied IS NULL)
	AND ([AuthorLinkName] = @AuthorLinkName OR @AuthorLinkName IS NULL)
	AND ([RefID] = @RefId OR @RefId IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [CountryID]
	, [AuthorName]
	, [AuthorFirstName]
	, [AuthorLastName]
	, [AuthorBirthday]
	, [AuthorType]
	, [AuthorDied]
	, [AuthorLinkName]
	, [RefID]
    FROM
	[dbo].[Authors]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([CountryID] = @CountryId AND @CountryId is not null)
	OR ([AuthorName] = @AuthorName AND @AuthorName is not null)
	OR ([AuthorFirstName] = @AuthorFirstName AND @AuthorFirstName is not null)
	OR ([AuthorLastName] = @AuthorLastName AND @AuthorLastName is not null)
	OR ([AuthorBirthday] = @AuthorBirthday AND @AuthorBirthday is not null)
	OR ([AuthorType] = @AuthorType AND @AuthorType is not null)
	OR ([AuthorDied] = @AuthorDied AND @AuthorDied is not null)
	OR ([AuthorLinkName] = @AuthorLinkName AND @AuthorLinkName is not null)
	OR ([RefID] = @RefId AND @RefId is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Tags_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Tags_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Tags_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Tags table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Tags_Get_List

AS


				
				SELECT
					[ID],
					[TagName]
				FROM
					[dbo].[Tags]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Tags_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Tags_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Tags_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Tags table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Tags_GetPaged
(

	@WhereClause varchar (8000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[Tags]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ID], O.[TagName]
				FROM
				    [dbo].[Tags] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
                
				-- get row count
				SET @SQL = 'SELECT COUNT(1) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Tags]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Tags_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Tags_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Tags_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Tags table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Tags_Insert
(

	@Id int    OUTPUT,

	@TagName nvarchar (100)  
)
AS


				
				INSERT INTO [dbo].[Tags]
					(
					[TagName]
					)
				VALUES
					(
					@TagName
					)
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Tags_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Tags_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Tags_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Tags table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Tags_Update
(

	@Id int   ,

	@TagName nvarchar (100)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Tags]
				SET
					[TagName] = @TagName
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Tags_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Tags_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Tags_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Tags table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Tags_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Tags] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Tags_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Tags_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Tags_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Tags table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Tags_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[TagName]
				FROM
					[dbo].[Tags]
				WHERE
					[ID] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Tags_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Tags_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Tags_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Tags table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Tags_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@TagName nvarchar (100)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [TagName]
    FROM
	[dbo].[Tags]
    WHERE 
	 ([ID] = @Id OR @Id IS NULL)
	AND ([TagName] = @TagName OR @TagName IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [TagName]
    FROM
	[dbo].[Tags]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([TagName] = @TagName AND @TagName is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Quotations_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Quotations_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Quotations_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Quotations table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Quotations_Get_List

AS


				
				SELECT
					[ID],
					[TopicID],
					[TagID],
					[AuthorID],
					[Quotes],
					[CreateDate],
					[UpdateDate]
				FROM
					[dbo].[Quotations]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Quotations_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Quotations_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Quotations_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Quotations table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Quotations_GetPaged
(

	@WhereClause varchar (8000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[Quotations]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ID], O.[TopicID], O.[TagID], O.[AuthorID], O.[Quotes], O.[CreateDate], O.[UpdateDate]
				FROM
				    [dbo].[Quotations] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
                
				-- get row count
				SET @SQL = 'SELECT COUNT(1) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Quotations]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Quotations_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Quotations_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Quotations_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Quotations table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Quotations_Insert
(

	@Id int    OUTPUT,

	@TopicId int   ,

	@TagId int   ,

	@AuthorId int   ,

	@Quotes nvarchar (MAX)  ,

	@CreateDate datetime   ,

	@UpdateDate datetime   
)
AS


				
				INSERT INTO [dbo].[Quotations]
					(
					[TopicID]
					,[TagID]
					,[AuthorID]
					,[Quotes]
					,[CreateDate]
					,[UpdateDate]
					)
				VALUES
					(
					@TopicId
					,@TagId
					,@AuthorId
					,@Quotes
					,@CreateDate
					,@UpdateDate
					)
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Quotations_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Quotations_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Quotations_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Quotations table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Quotations_Update
(

	@Id int   ,

	@TopicId int   ,

	@TagId int   ,

	@AuthorId int   ,

	@Quotes nvarchar (MAX)  ,

	@CreateDate datetime   ,

	@UpdateDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Quotations]
				SET
					[TopicID] = @TopicId
					,[TagID] = @TagId
					,[AuthorID] = @AuthorId
					,[Quotes] = @Quotes
					,[CreateDate] = @CreateDate
					,[UpdateDate] = @UpdateDate
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Quotations_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Quotations_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Quotations_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Quotations table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Quotations_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Quotations] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Quotations_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Quotations_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Quotations_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Quotations table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Quotations_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[TopicID],
					[TagID],
					[AuthorID],
					[Quotes],
					[CreateDate],
					[UpdateDate]
				FROM
					[dbo].[Quotations]
				WHERE
					[ID] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Quotations_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Quotations_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Quotations_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Quotations table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Quotations_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@TopicId int   = null ,

	@TagId int   = null ,

	@AuthorId int   = null ,

	@Quotes nvarchar (MAX)  = null ,

	@CreateDate datetime   = null ,

	@UpdateDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [TopicID]
	, [TagID]
	, [AuthorID]
	, [Quotes]
	, [CreateDate]
	, [UpdateDate]
    FROM
	[dbo].[Quotations]
    WHERE 
	 ([ID] = @Id OR @Id IS NULL)
	AND ([TopicID] = @TopicId OR @TopicId IS NULL)
	AND ([TagID] = @TagId OR @TagId IS NULL)
	AND ([AuthorID] = @AuthorId OR @AuthorId IS NULL)
	AND ([Quotes] = @Quotes OR @Quotes IS NULL)
	AND ([CreateDate] = @CreateDate OR @CreateDate IS NULL)
	AND ([UpdateDate] = @UpdateDate OR @UpdateDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [TopicID]
	, [TagID]
	, [AuthorID]
	, [Quotes]
	, [CreateDate]
	, [UpdateDate]
    FROM
	[dbo].[Quotations]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([TopicID] = @TopicId AND @TopicId is not null)
	OR ([TagID] = @TagId AND @TagId is not null)
	OR ([AuthorID] = @AuthorId AND @AuthorId is not null)
	OR ([Quotes] = @Quotes AND @Quotes is not null)
	OR ([CreateDate] = @CreateDate AND @CreateDate is not null)
	OR ([UpdateDate] = @UpdateDate AND @UpdateDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Log_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Log_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Log_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Log table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Log_Get_List

AS


				
				SELECT
					[ID],
					[Error],
					[Exception],
					[CreateDate]
				FROM
					[dbo].[Log]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Log_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Log_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Log_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Log table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Log_GetPaged
(

	@WhereClause varchar (8000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[Log]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ID], O.[Error], O.[Exception], O.[CreateDate]
				FROM
				    [dbo].[Log] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
                
				-- get row count
				SET @SQL = 'SELECT COUNT(1) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Log]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Log_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Log_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Log_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Log table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Log_Insert
(

	@Id int    OUTPUT,

	@Error nvarchar (MAX)  ,

	@Exception nvarchar (MAX)  ,

	@CreateDate datetime   
)
AS


				
				INSERT INTO [dbo].[Log]
					(
					[Error]
					,[Exception]
					,[CreateDate]
					)
				VALUES
					(
					@Error
					,@Exception
					,@CreateDate
					)
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Log_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Log_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Log_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Log table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Log_Update
(

	@Id int   ,

	@Error nvarchar (MAX)  ,

	@Exception nvarchar (MAX)  ,

	@CreateDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Log]
				SET
					[Error] = @Error
					,[Exception] = @Exception
					,[CreateDate] = @CreateDate
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Log_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Log_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Log_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Log table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Log_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Log] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Log_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Log_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Log_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Log table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Log_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[Error],
					[Exception],
					[CreateDate]
				FROM
					[dbo].[Log]
				WHERE
					[ID] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Log_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Log_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Log_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Log table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Log_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@Error nvarchar (MAX)  = null ,

	@Exception nvarchar (MAX)  = null ,

	@CreateDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [Error]
	, [Exception]
	, [CreateDate]
    FROM
	[dbo].[Log]
    WHERE 
	 ([ID] = @Id OR @Id IS NULL)
	AND ([Error] = @Error OR @Error IS NULL)
	AND ([Exception] = @Exception OR @Exception IS NULL)
	AND ([CreateDate] = @CreateDate OR @CreateDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [Error]
	, [Exception]
	, [CreateDate]
    FROM
	[dbo].[Log]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([Error] = @Error AND @Error is not null)
	OR ([Exception] = @Exception AND @Exception is not null)
	OR ([CreateDate] = @CreateDate AND @CreateDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Country_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Country_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Country_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Country table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Country_Get_List

AS


				
				SELECT
					[ID],
					[CountryName],
					[CountryCode]
				FROM
					[dbo].[Country]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Country_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Country_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Country_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Country table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Country_GetPaged
(

	@WhereClause varchar (8000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[Country]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ID], O.[CountryName], O.[CountryCode]
				FROM
				    [dbo].[Country] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
                
				-- get row count
				SET @SQL = 'SELECT COUNT(1) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Country]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Country_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Country_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Country_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Country table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Country_Insert
(

	@Id int    OUTPUT,

	@CountryName nvarchar (100)  ,

	@CountryCode varchar (5)  
)
AS


				
				INSERT INTO [dbo].[Country]
					(
					[CountryName]
					,[CountryCode]
					)
				VALUES
					(
					@CountryName
					,@CountryCode
					)
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Country_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Country_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Country_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Country table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Country_Update
(

	@Id int   ,

	@CountryName nvarchar (100)  ,

	@CountryCode varchar (5)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Country]
				SET
					[CountryName] = @CountryName
					,[CountryCode] = @CountryCode
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Country_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Country_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Country_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Country table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Country_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Country] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Country_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Country_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Country_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Country table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Country_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[CountryName],
					[CountryCode]
				FROM
					[dbo].[Country]
				WHERE
					[ID] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Country_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Country_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Country_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Country table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Country_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@CountryName nvarchar (100)  = null ,

	@CountryCode varchar (5)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [CountryName]
	, [CountryCode]
    FROM
	[dbo].[Country]
    WHERE 
	 ([ID] = @Id OR @Id IS NULL)
	AND ([CountryName] = @CountryName OR @CountryName IS NULL)
	AND ([CountryCode] = @CountryCode OR @CountryCode IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [CountryName]
	, [CountryCode]
    FROM
	[dbo].[Country]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([CountryName] = @CountryName AND @CountryName is not null)
	OR ([CountryCode] = @CountryCode AND @CountryCode is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AuthorTopic_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AuthorTopic_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AuthorTopic_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the AuthorTopic table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AuthorTopic_Get_List

AS


				
				SELECT
					[ID],
					[TopicID],
					[AuthorID]
				FROM
					[dbo].[AuthorTopic]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AuthorTopic_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AuthorTopic_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AuthorTopic_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the AuthorTopic table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AuthorTopic_GetPaged
(

	@WhereClause varchar (8000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[AuthorTopic]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ID], O.[TopicID], O.[AuthorID]
				FROM
				    [dbo].[AuthorTopic] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
                
				-- get row count
				SET @SQL = 'SELECT COUNT(1) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[AuthorTopic]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AuthorTopic_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AuthorTopic_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AuthorTopic_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the AuthorTopic table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AuthorTopic_Insert
(

	@Id int   ,

	@TopicId int   ,

	@AuthorId int   
)
AS


				
				INSERT INTO [dbo].[AuthorTopic]
					(
					[ID]
					,[TopicID]
					,[AuthorID]
					)
				VALUES
					(
					@Id
					,@TopicId
					,@AuthorId
					)
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AuthorTopic_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AuthorTopic_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AuthorTopic_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the AuthorTopic table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AuthorTopic_Update
(

	@Id int   ,

	@OriginalId int   ,

	@TopicId int   ,

	@AuthorId int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[AuthorTopic]
				SET
					[ID] = @Id
					,[TopicID] = @TopicId
					,[AuthorID] = @AuthorId
				WHERE
[ID] = @OriginalId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AuthorTopic_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AuthorTopic_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AuthorTopic_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the AuthorTopic table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AuthorTopic_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[AuthorTopic] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AuthorTopic_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AuthorTopic_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AuthorTopic_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the AuthorTopic table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AuthorTopic_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[TopicID],
					[AuthorID]
				FROM
					[dbo].[AuthorTopic]
				WHERE
					[ID] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AuthorTopic_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AuthorTopic_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AuthorTopic_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the AuthorTopic table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AuthorTopic_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@TopicId int   = null ,

	@AuthorId int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [TopicID]
	, [AuthorID]
    FROM
	[dbo].[AuthorTopic]
    WHERE 
	 ([ID] = @Id OR @Id IS NULL)
	AND ([TopicID] = @TopicId OR @TopicId IS NULL)
	AND ([AuthorID] = @AuthorId OR @AuthorId IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [TopicID]
	, [AuthorID]
    FROM
	[dbo].[AuthorTopic]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([TopicID] = @TopicId AND @TopicId is not null)
	OR ([AuthorID] = @AuthorId AND @AuthorId is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Topics_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Topics_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Topics_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Topics table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Topics_Get_List

AS


				
				SELECT
					[ID],
					[AuthorID],
					[TopicName],
					[TopicType]
				FROM
					[dbo].[Topics]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Topics_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Topics_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Topics_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Topics table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Topics_GetPaged
(

	@WhereClause varchar (8000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ID]'
				SET @SQL = @SQL + ' FROM [dbo].[Topics]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ID], O.[AuthorID], O.[TopicName], O.[TopicType]
				FROM
				    [dbo].[Topics] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ID] = PageIndex.[ID]
				ORDER BY
				    PageIndex.IndexId
                
				-- get row count
				SET @SQL = 'SELECT COUNT(1) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Topics]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Topics_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Topics_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Topics_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Topics table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Topics_Insert
(

	@Id int    OUTPUT,

	@AuthorId int   ,

	@TopicName nvarchar (200)  ,

	@TopicType nvarchar (100)  
)
AS


				
				INSERT INTO [dbo].[Topics]
					(
					[AuthorID]
					,[TopicName]
					,[TopicType]
					)
				VALUES
					(
					@AuthorId
					,@TopicName
					,@TopicType
					)
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Topics_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Topics_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Topics_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Topics table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Topics_Update
(

	@Id int   ,

	@AuthorId int   ,

	@TopicName nvarchar (200)  ,

	@TopicType nvarchar (100)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Topics]
				SET
					[AuthorID] = @AuthorId
					,[TopicName] = @TopicName
					,[TopicType] = @TopicType
				WHERE
[ID] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Topics_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Topics_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Topics_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Topics table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Topics_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Topics] WITH (ROWLOCK) 
				WHERE
					[ID] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Topics_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Topics_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Topics_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Topics table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Topics_GetById
(

	@Id int   
)
AS


				SELECT
					[ID],
					[AuthorID],
					[TopicName],
					[TopicType]
				FROM
					[dbo].[Topics]
				WHERE
					[ID] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Topics_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Topics_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Topics_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Topics table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Topics_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@AuthorId int   = null ,

	@TopicName nvarchar (200)  = null ,

	@TopicType nvarchar (100)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ID]
	, [AuthorID]
	, [TopicName]
	, [TopicType]
    FROM
	[dbo].[Topics]
    WHERE 
	 ([ID] = @Id OR @Id IS NULL)
	AND ([AuthorID] = @AuthorId OR @AuthorId IS NULL)
	AND ([TopicName] = @TopicName OR @TopicName IS NULL)
	AND ([TopicType] = @TopicType OR @TopicType IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ID]
	, [AuthorID]
	, [TopicName]
	, [TopicType]
    FROM
	[dbo].[Topics]
    WHERE 
	 ([ID] = @Id AND @Id is not null)
	OR ([AuthorID] = @AuthorId AND @AuthorId is not null)
	OR ([TopicName] = @TopicName AND @TopicName is not null)
	OR ([TopicType] = @TopicType AND @TopicType is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

